package com.sub.controller;

import com.sub.model.SpFirstMenu;
import com.sub.service.FirstMenuService;
import com.sub.utils.JSONResult;
import com.sub.utils.LogUtil;
import com.sub.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("first")
public class FirstMenuController {

	@Autowired
	private FirstMenuService firstMenuService;


	/**
	 * @Description: 展示一级菜单
	 */
	@PostMapping("/list")
	public JSONResult list(SpFirstMenu spFirstMenu) throws Exception {
		List<SpFirstMenu> list = firstMenuService.queryFirstMenuList(spFirstMenu);
		return JSONResult.ok(list);
	}

	/**
	 * @Description: 展示一级菜单(条件分页)
	 */
	@PostMapping("/pagelist")
	public JSONResult pagelist(String name,int page) throws Exception {
		PageList pageList = new PageList();
		pageList = firstMenuService.queryFirstMenuByPageAndName(pageList,page,name);
		return JSONResult.ok(pageList);
	}

	/**
	 * @Description: 新增一级菜单
	 */
	@PostMapping("/add")
	public JSONResult add(SpFirstMenu firstMenu) throws Exception {
		int result = firstMenuService.saveFirst(firstMenu);
		if(result==1){
			LogUtil.info("新增成功");
			return JSONResult.ok();
		}else {
			LogUtil.info("新增失败");
			return JSONResult.errorMsg("失败");
		}
	}

	/**
	 * @Description: 新增一级菜单
	 */
	@PostMapping("/edit")
	public JSONResult edit(SpFirstMenu firstMenu) throws Exception {
		int result = firstMenuService.editFirst(firstMenu);
		if(result==1){
			LogUtil.info("修改成功");
			return JSONResult.ok();
		}else {
			LogUtil.info("修改失败");
			return JSONResult.errorMsg("失败");
		}
	}

	/**
	 * @Description: 删除一级菜单（单个）
	 */
	@PostMapping("/remove")
	public JSONResult remove(String firstMenuId) throws Exception {
		int result = firstMenuService.removeFirstById(firstMenuId);
		if(result==1){
			LogUtil.info("删除成功");
			return JSONResult.ok();
		}else {
			LogUtil.info("删除失败");
			return JSONResult.errorMsg("失败");
		}
	}

	/**
	 * @Description: 删除一级菜单（批量）
	 */
	@PostMapping("/batchremove")
	public JSONResult batchremove(String ids) throws Exception {
		int result = firstMenuService.batchRemoveFirstByIdS(ids.split(","));
		if(result>0){
			LogUtil.info("删除成功");
			return JSONResult.ok();
		}else {
			LogUtil.info("删除失败");
			return JSONResult.errorMsg("失败");
		}
	}
}
